<?php
require_once "ModelBase.php";

class My extends TTEvent_Table_Abstract
{
	protected $_name = 'tamtay_event_member';
	protected $_primary = array('uid', 'eid');
	
	public function getMyEvent($uid, $timeIndex='all', $page=-1, $limit=20){
		$uid = (int)$uid;
		if(!$uid)
			return array();
		$sql = "";
		$now = time();
		switch($timeIndex){
			case 'all':
				$sql = "SELECT * , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE m.uid=".$uid;
				break;
			case 'ongoing':
				$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND m.uid=".$uid;
				break;
			case 'upcoming':
				$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE e.start_time > ".$now." AND m.uid=".$uid;
				break;
			case 'past':
				$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE e.end_time < ".$now." AND m.uid=".$uid;
				break;
		}
		$order = " ORDER BY e.changed DESC";
		if($page >=0){
			//$this->setRowPerPage($limit);
			//return $this->pageQuery($sql.$order, null, $page);
			return $this->pageQuery2($sql.$order, null, $page, $limit);
		}
		else return $this->execSelectQuery($sql.$order);
	}
	
	public function getCountMyEvent($uid, $timeIndex='all'){
		$uid = (int)$uid;
		if(!$uid)
			return 0;
		$sql = "";
		$now = time();
		switch($timeIndex){
			case 'all':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					WHERE m.uid=".$uid;
				break;
			case 'ongoing':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND m.uid=".$uid;
				break;
			case 'upcoming':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					WHERE e.start_time > ".$now." AND m.uid=".$uid;
				break;
			case 'past':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
					WHERE e.end_time < ".$now." AND m.uid=".$uid;
				break;
		}
		
		$db = $this->getSlaveDB();
		return $db->fetchOne($sql);
	}
	
	public function getMyOwnEvent($uid, $timeIndex='all', $page=-1, $limit=20){
		$uid = (int)$uid;
		if(!$uid)
			return array();
		$sql = "";
		$now = time();
		switch($timeIndex){
			case 'all':
				$sql = "SELECT * , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE e.uid=".$uid;
				break;
			case 'ongoing':
				$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND e.uid=".$uid;
				break;
			case 'upcoming':
				$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE e.start_time > ".$now." AND e.uid=".$uid;
				break;
			case 'past':
				$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					INNER JOIN mytt_network n ON e.network_id=n.nwid
					INNER JOIN tamtay_term t ON e.cid=t.tid
					WHERE e.end_time < ".$now." AND e.uid=".$uid;
				break;
		}
		$order = " ORDER BY e.changed DESC";
		if($page >=0){
			//$this->setRowPerPage($limit);
			//return $this->pageQuery($sql.$order, null, $page);
			return $this->pageQuery2($sql.$order, null, $page, $limit);
		}
		else return $this->execSelectQuery($sql.$order);
	}
	
	public function getCountMyOwnEvent($uid, $timeIndex='all'){
		$uid = (int)$uid;
		if(!$uid)
			return 0;
		$sql = "";
		$now = time();
		switch($timeIndex){
			case 'all':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					WHERE e.uid=".$uid;
				break;
			case 'ongoing':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND e.uid=".$uid;
				break;
			case 'upcoming':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					WHERE e.start_time > ".$now." AND e.uid=".$uid;
				break;
			case 'past':
				$sql = "SELECT COUNT(*)
					FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid AND m.uid=".$uid."
					WHERE e.end_time < ".$now." AND e.uid=".$uid;
				break;
		}
		
		$db = $this->getSlaveDB();
		return $db->fetchOne($sql);
	}
	
	
	public function getMyFriendEvent($uid, $fid, $timeIndex='all', $page=-1, $limit=20){
		$uid = (int)$uid;
		if(!$uid)
			return array();
		$sql = "";
		$now = time();
		if(is_numeric($fid)){
			$uid = $fid;
			switch($timeIndex){
				case 'all':
					$sql = "SELECT * , n.name as network_name, t.tid, t.name as category_name, m.admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE m.uid=".$uid;
					break;
				case 'ongoing':
					$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND m.uid=".$uid;
					break;
				case 'upcoming':
					$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE e.start_time > ".$now." AND m.uid=".$uid;
					break;
				case 'past':
					$sql = "SELECT *  , n.name as network_name, t.tid, t.name as category_name, m.admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE e.end_time < ".$now." AND m.uid=".$uid;
					break;
			}
		}
		else if(is_array($fid) && count($fid)>0){
			switch($timeIndex){
				case 'all':
					$sql = "SELECT DISTINCT e.* , n.name as network_name, t.tid, t.name as category_name, 0 as admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE m.uid IN ".$this->makeIn($fid);
					break;
				case 'ongoing':
					$sql = "SELECT DISTINCT e.*  , n.name as network_name, t.tid, t.name as category_name, 0 as admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND m.uid IN ".$this->makeIn($fid);
					break;
				case 'upcoming':
					$sql = "SELECT DISTINCT e.*  , n.name as network_name, t.tid, t.name as category_name, 0 as admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE e.start_time > ".$now." AND m.uid IN ".$this->makeIn($fid);
					break;
				case 'past':
					$sql = "SELECT DISTINCT e.*  , n.name as network_name, t.tid, t.name as category_name, 0 as admin
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						INNER JOIN mytt_network n ON e.network_id=n.nwid
						INNER JOIN tamtay_term t ON e.cid=t.tid
						WHERE e.end_time < ".$now." AND m.uid IN ".$this->makeIn($fid);
					break;
			}
		}
		//$order = " ORDER BY e.changed DESC";
		$order = "";
		$ret = array();
		if($sql != ""){
			if($page >=0){
				//$this->setRowPerPage($limit);
				//return $this->pageQuery($sql.$order, null, $page);
				$ret = $this->pageQuery2($sql.$order, null, $page, $limit);
			}
			else $ret = $this->execSelectQuery($sql.$order);
		}
		return $ret;
	}
	
	public function getCountMyFriendEvent($uid, $fid, $timeIndex='all'){
		$uid = (int)$uid;
		if(!$uid)
			return 0;
		$sql = "";
		$now = time();
		if(is_numeric($fid)){
			$uid  = $fid;
			switch($timeIndex){
				case 'all':
					$sql = "SELECT COUNT(*)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE m.uid=".$uid;
					break;
				case 'ongoing':
					$sql = "SELECT COUNT(*)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND m.uid=".$uid;
					break;
				case 'upcoming':
					$sql = "SELECT COUNT(*)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE e.start_time > ".$now." AND m.uid=".$uid;
					break;
				case 'past':
					$sql = "SELECT COUNT(*)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE e.end_time < ".$now." AND m.uid=".$uid;
					break;
				}
			}
		else if(is_array($fid) && count($fid)>0){
			switch($timeIndex){
				case 'all':
					$sql = "SELECT COUNT( DISTINCT e.eid)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE m.uid IN ".$this->makeIn($fid);
					break;
				case 'ongoing':
					$sql = "SELECT COUNT( DISTINCT e.eid)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE e.start_time <= ".$now." AND e.end_time >= ".$now." AND m.uid IN ".$this->makeIn($fid);
					break;
				case 'upcoming':
					$sql = "SELECT COUNT( DISTINCT e.eid)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE e.start_time > ".$now." AND m.uid IN ".$this->makeIn($fid);
					break;
				case 'past':
					$sql = "SELECT COUNT( DISTINCT e.eid)
						FROM tamtay_event e JOIN tamtay_event_member m ON e.eid=m.eid
						WHERE e.end_time < ".$now." AND m.uid IN ".$this->makeIn($fid);
					break;
			}
		}
		
		
		$ret = 0;
		if($sql != ""){
			$db = $this->getSlaveDB();
			$ret = $db->fetchOne($sql);
		}
		return $ret;
	}
	
	public function getMyEventInvited($uid, $status="", $page=0, $count=true){
		$statusCond = "";
		switch ($status){
			case 'maybe':
				$statusCond = " AND r.status = " . EVENT_INVITE_REQUEST_MAYBE_ATTENDING;
				break;
			case 'not':
				$statusCond = " AND r.status = " . EVENT_INVITE_REQUEST_NOT_ATTENDING;
				break;
			default:
				$statusCond = " AND r.status = " . EVENT_INVITE_REQUEST_SENT;
				break;
		}
		$sql="SELECT e.eid, e.event_name, e.logo, e.member_count, e.network_id, e.start_time, e.end_time, e.website, n.name as network_name, td.tid, td.name AS category_name, r.status 
			FROM tamtay_request r 
				INNER JOIN tamtay_event e ON r.sender_id=e.eid
				INNER JOIN mytt_network n ON e.network_id = n.nwid
				INNER JOIN tamtay_term td ON e.cid=td.tid 
			WHERE r.receiver_id=$uid AND r.type=" . REQUEST_EVENT_INVITE_TYPE . $statusCond." 
			ORDER BY e.changed DESC ";
		
		if($count===true){
			$count = "SELECT count(*) 
				FROM tamtay_request r 
					INNER JOIN tamtay_event e ON r.sender_id=e.eid
					INNER JOIN tamtay_term td ON e.cid=td.tid 
				WHERE r.receiver_id=$uid AND r.type=" . REQUEST_EVENT_INVITE_TYPE . $statusCond;
		}
		else $count = (int)$count;
		
		
		return $this->pageQuery($sql, null, $page, $count);
	}
	
	public function getCountMyEventInvited($uid, $status=""){
		$statusCond = "";
		switch ($status){
			case 'maybe':
				$statusCond = " AND r.status = " . EVENT_INVITE_REQUEST_MAYBE_ATTENDING;
				break;
			case 'not':
				$statusCond = " AND r.status = " . EVENT_INVITE_REQUEST_NOT_ATTENDING;
				break;
			default:
				$statusCond = " AND r.status = " . EVENT_INVITE_REQUEST_SENT;
				break;
		}
		$slqCount = "SELECT count(*) 
			FROM tamtay_request r 
				INNER JOIN tamtay_event e ON r.sender_id=e.eid
				INNER JOIN tamtay_term td ON e.cid=td.tid 
			WHERE r.receiver_id=$uid AND r.type=" . REQUEST_EVENT_INVITE_TYPE . $statusCond;
		
		$db = $this->getSlaveDB();
		return $db->fetchOne($slqCount);
	}
}

